package com.lgw.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgw.demo.dao.UserMapper;
import com.lgw.demo.pojo.User;
import com.lgw.demo.service.UserServcie;

@Controller
public class User1Controller {

    //查询
    @Autowired//注入
    UserServcie userServcie;
    //Controller->Servcie->Dao
    @RequestMapping("userlist")
    public String getAllUser(Model model,String name){
        if (name!=null){
            model.addAttribute("list",userServcie.searchUserByName(name));
        }else
        {
            model.addAttribute("list",userServcie.getAllUser());
        }
        return "userlist.html";
    }

    @RequestMapping("adduser")
    public String adduser(){
        return "adduser.html";
    }

    @RequestMapping("addcommit")
    public String addcommit(User user){
        int result= userServcie.addUser(user);
        if (result>0){
            return "redirect:/userlist";
        }
        return "fail.html";
    }

    @RequestMapping("deleteUserById")
    public String deleteUserById(Integer id){
        int result= userServcie.deleteUserById(id);
        if (result>0){
            return "redirect:/userlist";
        }
        return "fail.html";
    }

    @RequestMapping("updateUser")
    public String updateUser(Model model,int id,int age,String name){
        model.addAttribute("id",id);
        model.addAttribute("age",age);
        model.addAttribute("name",name);
        return "updateuser.html";
    }

    @RequestMapping("updatecommit")
    public String updatecommit(User user){
        int result= userServcie.updateUser(user);
        if (result>0){
            return "redirect:/userlist";
        }
        return "fail.html";
    }

}
